import json
import traceback

import facade
import pymysql
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "rucjournalold", "download", "listpage")
dbconfigpath = BaseDir.get_new_path(curPath, "db.ini")


class ParaListPage():
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(dbconfigpath, "db", logger=self.logger)

    def read_big_file(self):
        for file in BaseDir.get_dir_all_files(sPath):
            for lines in BaseDir.read_file_r_mode_yield(file):
                try:
                    data = json.loads(lines)
                    ListArticle = data["ListArticle"]
                    for article in ListArticle:
                        id = article["Id"]
                        jsonmsg = json.dumps(article, ensure_ascii=False)
                        sql = "insert ignore into `articleold` (`rawid`,`jsonmsg`) values ('{}','{}')".format(id,
                                                                                                              pymysql.escape_string(
                                                                                                                  jsonmsg))
                        self.mysqlutils.ExeSqlToDB(sql)
                except:
                    traceback.print_exc()
                    print(lines)


if __name__ == "__main__":
    para = ParaListPage()
    para.read_big_file()
